<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click.native="showdidlong=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              {{ formatEmployment(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry">
            <template #default="{row}">
              {{ formaDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="dodele(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 弹层 -->
      <el-dialog title="收货地址" :visible.sync="showdidlong" @close="hclose">
        <empDialog ref="daref" @suceess="hsucc" @add="ha" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import PageTools from '@/components/PageTools'
import { getEmployeeList, delEmployee } from '@/api/employees'
import employees from '@/constant/employees'
import empDialog from './empDialog.vue'
import dayjs from 'dayjs'
export default {
  components: {
    PageTools,
    empDialog
  },
  data() {
    return {
      // 弹层开管
      showdidlong: false,
      // 分页组件内容
      page: 1,
      size: 5,
      total: 0,
      //   渲染数据
      list: [],
      hireType: employees.hireType
    }
  },
  created() {
    this.loadEmployeeList()
  },
  methods: {
    // 聘用格式
    formatEmployment(val) {
      const hireType = {}
      employees.hireType.forEach(item => { hireType[item.id] = item.value })
      return hireType[val] ? hireType[val] : '未知'
    },
    // 每页条数
    handleSizeChange(val) {
      this.size = val
      this.loadEmployeeList()
    },
    // 页数
    handleCurrentChange(val) {
      this.page = val
      this.loadEmployeeList()
    },
    // 全部员工
    async loadEmployeeList() {
      try {
        const res = await getEmployeeList(this.page, this.size)
        this.list = res.data.rows
        this.total = res.data.total
        console.log(res, '全部员工')
      } catch (error) {
        console.log(error)
      }
    },
    // 删除函数
    async deleteEmployee(id) {
      try {
        await delEmployee(id)
        if (this.list.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 确定删除
    dodele(id) {
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteEmployee(id)
        this.loadEmployeeList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hsucc() {
      this.total++
      this.page = Math.ceil(this.total / this.size)
      this.showdidlong = false
      this.loadEmployeeList()
    },
    ha() {
      this.showdidlong = false
    },
    hclose() {
      this.$refs.daref.reseform()
    },
    // 格式化时间
    formaDate(timer) {
      return dayjs(timer).format('YYYY-MM-DD')
    }

  }
}
</script>
